package com.czx.controller;

import com.czx.common.constants.CookieKey;
import com.czx.common.constants.RedisKeys;
import com.czx.entity.DiscussPost;
import com.czx.entity.MyPage;
import com.czx.entity.User;
import com.czx.common.security.UserTokenThreadLocal;
import com.czx.service.CommentService;
import com.czx.service.DiscussPostService;
import com.czx.common.utils.R;
import com.czx.vo.DiscussPostVo;
import com.czx.vo.UserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DiscussPostController {
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private CommentService commentService;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/publish")
    @ResponseBody
    public R DisscussPublish(@RequestBody DiscussPost discussPost){
        UserToken userToken = UserTokenThreadLocal.get();
        User user = userToken.getUser();
        R r=discussPostService.addDisscuss(user,discussPost);
        System.out.println(discussPost);
        return r;
    }

    /**
     * 帖子详情
     * @param page
     * @param pid
     * @param model
     * @return
     */
    @RequestMapping("/disDetail")
    //帖子id
    public String disDetail(MyPage page,Integer pid, Model model){
        UserToken userToken = UserTokenThreadLocal.get();
        String key=null;
        //帖子的key
        key=String.format(RedisKeys.USER_LIKE_KEY, CookieKey.POST,pid);
        SetOperations<String, String> setOperations = stringRedisTemplate.opsForSet();
        Boolean member = setOperations.isMember(key, userToken.getUser().getId().toString());
        if (member) {
            model.addAttribute("likeStatus",1);

        }else {
            model.addAttribute("likeStatus",0);
        }
        //获取点赞数量
        Long size = stringRedisTemplate.opsForSet().size(key);
        model.addAttribute("likeCount",size);
        //帖子详情展示
       DiscussPostVo discussPostVo= discussPostService.disDetail(pid);
       model.addAttribute("topic",discussPostVo);

       //帖子回复列表
        //评论的key
        key = String.format(RedisKeys.USER_LIKE_KEY, CookieKey.COMMENT, pid);

        page=commentService.disDetailReplay(page,pid,key,userToken);
        model.addAttribute("page",page);
        page.setPath("/");
        return "site/discuss-detail";
    }
    @RequestMapping("/search")
    public String search(MyPage page,String keyword,Model model){
        page = discussPostService.searchPost(keyword, page);
        model.addAttribute("page",page);
        page.setPath("/index");
        return "index";

    }



}
